package com.sj.parksdk.activity;

import android.os.Bundle;

import com.sj.parksdk.R;
import com.sj.parksdk.core.BaseActivity;
import com.sj.parksdk.fragment.parking.FragmentQrCodePay;
import com.sj.parksdk.fragment.parking.FragmentStartPark;
import com.sj.parksdk.fragment.profile.FragmentReceiveCoupons;
import com.sj.parksdk.setting.Dictionary;
import com.sj.parksdk.setting.Extras;
import com.sj.parksdk.utils.Utils;
import com.sj.parksdk.widget.sweetalert.SweetAlertDialog;
import com.xuexiang.xhttp2.logs.HttpLog;
import com.xuexiang.xpage.core.PageOption;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.utils.ResUtils;
import com.xuexiang.xutil.common.StringUtils;

import java.util.Map;

public class ActivityQrCodeSuccess extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initScanResult();
    }

    private void initScanResult() {
        String url = getIntent().getStringExtra(Extras.BASE_RESULT);
        if (StringUtils.isEmpty(url)) {
            showErrorDialog("二维码内容为空");
            return;
        }
        if (!url.contains("type")) {
            showErrorDialog("二维码类型错误");
            return;
        }
//        Map<String, String> params = HttpUtils.getUrlParams(url);
        Map<String, String> params = Utils.url2Map(url);
        if(params == null){
            showErrorDialog("二维码类型错误");
            return;
        }
        HttpLog.e(params.toString());
        String type = params.get("type");
        HttpLog.e("type:"+params.get("type"));
        HttpLog.e("couponCode:"+params.get("couponCode"));
        if(StringUtils.isEmpty(type)){
            showErrorDialog("二维码类型错误");
            return;
        }
        switch (type){
            case Dictionary.QRCode.RECEIVE_COUPONS:
                gotoReceiveCoupons(params.get("couponCode"));
                break;
            case Dictionary.QRCode.PREPAYMENT:
                gotoPay(params.get("parkingLotId"));
                break;
            default:
                showErrorDialog("二维码类型错误");
                break;
        }
//        if(StringUtils.isEmpty(type))return;
//        if("Success".equals(type)){
//            gotoStartPark();
//        }else if("Pay".equals(type)){
//            gotoPay(params.get("parkingLotId"));
//        }
    }

    private void showErrorDialog(String message) {
        new SweetAlertDialog(this,SweetAlertDialog.ERROR_TYPE)
                .setContentMinHeight(DensityUtils.dp2px(200))
                .setTitleText("")
                .setContentText(message)
                .setConfirmText(ResUtils.getString(R.string.dialog_button_sure))
                .showCancelButton(false)
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismiss();
                        finish();
                    }
                }).show();
    }

    private void gotoReceiveCoupons(String couponCode) {
        PageOption.to(FragmentReceiveCoupons.class)
                .setAnim(CoreAnim.none)
                .putString(Extras.BASE_CODE,couponCode)
                .open(this);
    }

    private void gotoPay(String parkingLotId) {
        if(StringUtils.isEmpty(parkingLotId))return;
        long id = Long.valueOf(parkingLotId);
        PageOption.to(FragmentQrCodePay.class)
                .setAnim(CoreAnim.none)
                .putLong(Extras.PARK_PARKLOT_ID,id)
                .open(this);
    }

    private void gotoStartPark() {
        PageOption.to(FragmentStartPark.class)
                .setAnim(CoreAnim.none)
                .open(this);
    }

    @Override
    public void onBackPressed() {
        if (this.getSupportFragmentManager().getBackStackEntryCount() == 1) {
            Utils.syncMainPageStatus();
        }
        super.onBackPressed();
    }
}
